package com.ruitu.health_1.ui;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.ruitu.arad.Arad;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.health_1.R;
import com.ruitu.health_1.dao.bean.OrderItemList;
import com.ruitu.health_1.mvp.model.OrderModel;
import com.ruitu.health_1.mvp.presenter.ShippedPresenter;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.Logg;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.router_module.util.ReqUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShippedActivity extends BaseActivity<ShippedPresenter.ShippedP, OrderModel.OrderM> {

    private RecyclerView recyclerview;
    private CheckBox cb_check_all;
    private TextView tv_all_shipped;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_shipped;
    }

    ArrayList<OrderItemList> orderItems;
    private DataAdapter adapter;

    @Override
    protected void initViews() {
        super.initViews();
        setHeadTitle("选择确认收货");
        orderItems = (ArrayList<OrderItemList>) getIntent().getExtras().get("order");
        Logg.i("order:  " + orderItems.toString());
        recyclerview = findViewById(R.id.recyclerview);
        cb_check_all = findViewById(R.id.cb_check_all);
        tv_all_shipped = findViewById(R.id.tv_all_shipped);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(manager);
        adapter = new DataAdapter(this);
        recyclerview.setAdapter(adapter);
        adapter.setDataList(orderItems);

        setListener();
    }

    private void setListener() {
        cb_check_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean checked = cb_check_all.isChecked();
                for (int i = 0; i < orderItems.size(); i++) {
                    orderItems.get(i).setCheck(checked);
                }
                adapter.notifyDataSetChanged();
            }
        });
        tv_all_shipped.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isDataEmpty(ids())) {
                    ToastUtils.showShort("请选择确认收货的商品");
                    return;
                }
                Map map = new HashMap();
                map.put("ids", ids());
                map.put("memberId", AppHolder.instance().getUser().getId());
                showProgress();
                p.shipedd(ReqUtil.getBody(map, true));
            }
        });
    }

    private List list;

    private List ids() {
        list = new ArrayList();
        for (int i = 0; i < orderItems.size(); i++) {
            if (orderItems.get(i).isCheck()) {
                list.add(orderItems.get(i).getId());
            }
        }
        return list;
    }

    public void shippedSuc() {
        finish();
        Arad.bus.post(new EventModel(2020010800));
    }

    private void checkAll() {
        cb_check_all.setChecked(isAllCheck());
    }

    private boolean isAllCheck() {
        for (OrderItemList orderItemList : orderItems) {
            if (!orderItemList.isCheck()) {
                return false;
            }
        }
        return true;
    }

    class DataAdapter extends ListBaseAdapter<OrderItemList> {

        public DataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_shipped_list;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, final int position) {
            ImageView tv_radio = holder.getView(R.id.tv_radio);
            ImageView iv_product_img = holder.getView(R.id.iv_product_img);
            TextView tv_product_name = holder.getView(R.id.tv_product_name);
            TextView tv_sku_info = holder.getView(R.id.tv_sku_info);
            TextView tv_price = holder.getView(R.id.tv_price);
            RelativeLayout ll_item = holder.getView(R.id.ll_item);
            final OrderItemList orderItemList = mDataList.get(position);
            Glide.with(mContext).load(orderItemList.getProductPic()).into(iv_product_img);
            tv_product_name.setText(orderItemList.getProductName());
            StringBuffer buffer = new StringBuffer();
            if (!TextUtils.isEmpty(orderItemList.getGood().getSp1())) {
                buffer.append(orderItemList.getGood().getSp1());
            }
            if (!TextUtils.isEmpty(orderItemList.getGood().getSp2())) {
                buffer.append("," + orderItemList.getGood().getSp2());
            }
            if (!TextUtils.isEmpty(orderItemList.getGood().getSp3())) {
                buffer.append("," + orderItemList.getGood().getSp3());
            }
            tv_sku_info.setText(buffer.toString());
            tv_price.setText(orderItemList.getGood().getProductPrice());
            if (orderItemList.isCheck()) {
                tv_radio.setImageResource(R.mipmap.all_selects);
            } else {
                tv_radio.setImageResource(R.mipmap.all_noselects);
            }
            ll_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (orderItemList.isCheck()) {
                        orderItems.get(position).setCheck(false);
                    } else {
                        orderItems.get(position).setCheck(true);
                    }
                    checkAll();
                    notifyDataSetChanged();
                }
            });
        }
    }
}
